package com.etc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.AsMatter;
import com.etc.entity.AsUser;
import com.etc.entity.MatterImage;
import com.etc.service.IAsMatterService;
import com.etc.util.SMTPUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 素材管理Controller
 * 
 * @author ruoyi
 * @date 2022-11-27
 */
@RestController
@RequestMapping("/matter")
public class AsMatterController
{
    @Autowired
    private IAsMatterService asMatterService;

    /**
     * 查询素材管理列表
     */

    @GetMapping("/list")
    public PageInfo<AsMatter> list(@RequestParam(value = "keywords" , required = false,defaultValue = "")String keywords,@RequestParam(value = "offset",defaultValue = "1",required = false) int page,@RequestParam(value = "limit",defaultValue = "10",required = false) int limit)
    {

        Page<AsMatter> list = asMatterService.selectAsMatterList(keywords,page,limit);
        PageInfo<AsMatter> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 导出素材管理列表
     */


//    @PostMapping("/export")
//    public void export(HttpServletResponse response, AsMatter asMatter) throws IOException {
//        List<AsMatter> list = asMatterService.selectAsMatterList(asMatter);
//        ExcelUtil<AsMatter> util = new ExcelUtil<AsMatter>(AsMatter.class);
//        util.exportExcel(response, list, "素材管理数据");
//    }

    /**
     * 获取素材管理详细信息
     */

    @GetMapping(value = "/{id}")
    public List getInfo(@PathVariable("id") Long id)
    {
        return asMatterService.selectAsMatterById(id);
    }

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private Queue uploadMatter;
    /**
     * 新增素材管理
     */


    @PostMapping("uploadmatter")
    public AsMatter add(Integer id,Integer userid, MultipartFile myfile) throws IOException {

        //将图片上传给mq
        System.out.println(id+" "+userid+" "+myfile);
        //添加素材信息
        AsMatter asMatter = new AsMatter();
        asMatter.setUserid(userid);
        asMatter.setMattername(myfile.getOriginalFilename());
        AsMatter asMatter1 = asMatterService.insertAsMatter(asMatter, id);
        MatterImage matterImage = new MatterImage(myfile.getBytes(), myfile.getOriginalFilename(),asMatter1.getUser().getUserphone());
        amqpTemplate.convertAndSend(uploadMatter.getName(),matterImage);
        System.out.println(id+" "+userid+" "+myfile);
        return asMatter1;

    }


    /**
     * 修改素材管理
     */


    @PutMapping
    public AjaxResult edit(@RequestBody AsMatter asMatter)
    {
        return AjaxResult.success();
    }

    /**
     * 删除素材管理
     */


	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return AjaxResult.success();
    }
}
